package com.martin.dms.web.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.binding.message.MessageBuilder;
import org.springframework.binding.message.MessageContext;
import org.springframework.binding.validation.ValidationContext;

import com.martin.dms.model.Item;
import com.martin.dms.model.Supplier;

public class SupplierBean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -2500200217257269104L;

    private Long id;
    private String name;
    private String address;
    private String email;
    private String phoneNumber;
    private List<ItemBean> items = new ArrayList<ItemBean>();
    
    private List<Long> itemIds = new ArrayList<Long>();
    
    private String itemsNameFilter = "*";

    private String deliveryAmount = "0";

    public SupplierBean() {
        
    }
    
    public SupplierBean(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public SupplierBean(Supplier supplier) {
        this.id = supplier.getId();
        this.name = supplier.getName();
        this.address = supplier.getAddress();
        this.email = supplier.getEmail();
        this.phoneNumber = supplier.getPhoneNumber();
        items = new ArrayList<ItemBean>();
        for (Item item : supplier.getItems()) {
            items.add(new ItemBean(item.getId(), item.getName()));
        }
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param phoneNumber the phoneNumber to set
     */
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    /**
     * @return the phoneNumber
     */
    public String getPhoneNumber() {
        return phoneNumber;
    }

    /**
     * @param items the items to set
     */
    public void setItems(List<ItemBean> items) {
        this.items = items;
    }

    /**
     * @return the items
     */
    public List<ItemBean> getItems() {
        return items;
    }

    /**
     * @param itemIds the itemIds to set
     */
    public void setItemIds(List<Long> itemIds) {
        this.itemIds = itemIds;
    }

    /**
     * @return the itemIds
     */
    public List<Long> getItemIds() {
        return itemIds;
    }

    /**
     * @param itemsNameFilter the itemsNameFilter to set
     */
    public void setItemsNameFilter(String itemsNameFilter) {
        this.itemsNameFilter = itemsNameFilter;
    }

    /**
     * @return the itemsNameFilter
     */
    public String getItemsNameFilter() {
        return itemsNameFilter;
    }

    /**
     * @param deliveryAmount the deliveryAmount to set
     */
    public void setDeliveryAmount(String deliveryAmount) {
        this.deliveryAmount = deliveryAmount;
    }

    /**
     * @return the deliveryAmount
     */
    public String getDeliveryAmount() {
        return deliveryAmount;
    }
    
    public int getIntDeliveryAmount() {
        return Integer.valueOf(deliveryAmount);
    }

    public void validateSuppliersForm(ValidationContext validationContext) {
        MessageContext messageContext = validationContext.getMessageContext();
        String userEvent = validationContext.getUserEvent();
        if (userEvent.equals("save")) {
            if (name == null || "".equals(name)) {
                messageContext.addMessage(new MessageBuilder().error().code("error.emptyfield.name").build());
            } else if (email == null || "".equals(email)) {
                messageContext.addMessage(new MessageBuilder().error().code("error.emptyfield.email").build());
            } else if (address == null || "".equals(address)) {
                messageContext.addMessage(new MessageBuilder().error().code("error.emptyfield.address").build());
            } else if (phoneNumber == null || "".equals(phoneNumber)) {
                messageContext.addMessage(new MessageBuilder().error().code("error.emptyfield.phoneNumber").build());
            }
        }
    }
    
    public void validateSuppliersDetails(ValidationContext validationContext) {
        MessageContext messageContext = validationContext.getMessageContext();
        String userEvent = validationContext.getUserEvent();
        if (userEvent.equals("makeDelivery")) {
            if (itemIds == null || itemIds.size() != 1) {
                messageContext.addMessage(new MessageBuilder().error().code("error.selectOneItem").build());
            } else if (deliveryAmount == null || "".equals(deliveryAmount)) {
                messageContext.addMessage(new MessageBuilder().error().code("error.emptyfield.amount").build());
            } else {
                try {
                    Integer.valueOf(deliveryAmount);
                } catch (NumberFormatException nbe) {
                    messageContext.addMessage(new MessageBuilder().error().code("error.integerfield.amount").build());
                    return;
                }
                if (getIntDeliveryAmount() <= 0) {
                    messageContext.addMessage(new MessageBuilder().error().code("error.positivefield.amount").build());
                }
            }
        }
    }
}
